import { ApiResponse, Article, Channel } from '@/types/data'
import { HomeAction, RootThunkAction } from '@/types/store'
import instance from '@/utils/request'
import { getChannel, hasToken, setChannel } from '@/utils/storage'
import store from '..'

export const getUserChannel = (): RootThunkAction => {
  return async (dispatch) => {
    if (hasToken()) {
      // 已登录
      const res = await instance.get<ApiResponse<{ channels: Channel[] }>>(
        'user/channels'
      )
      //   console.log(res)
      dispatch({
        type: 'home/getUserChannel',
        payload: res.data.data.channels,
      })
    } else {
      // 未登录
      const channels = getChannel()
      if (channels.length > 0) {
        dispatch({
          type: 'home/getUserChannel',
          payload: channels,
        })
      } else {
        const res = await instance.get<ApiResponse<{ channels: Channel[] }>>(
          'user/channels'
        )
        dispatch({
          type: 'home/getUserChannel',
          payload: res.data.data.channels,
        })
        setChannel(res.data.data.channels)
      }
    }
  }
}

export const getAllChannel = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await instance.get<ApiResponse<{ channels: Channel[] }>>(
      '/channels'
    )
    // console.log(res)
    dispatch({
      type: 'home/getAllChannels',
      payload: res.data.data.channels,
    })
  }
}

export const changeActive = (id: number): HomeAction => {
  return {
    type: 'home/changeActive',
    payload: id,
  }
}

export const addUserChannel = (channels: Channel): RootThunkAction => {
  return async (dispatch, getState) => {
    const { userChannel } = getState().home
    if (hasToken()) {
      // 登录状态
      await instance.patch('/user/channels', {
        channels: [channels],
      })
    } else {
      // 游客状态
      setChannel([...userChannel])
    }
    dispatch({
      type: 'home/getUserChannel',
      payload: [...userChannel, channels],
    })
  }
}

export const delUserChannel = (id: number): RootThunkAction => {
  return async (dispatch, getState) => {
    const { userChannel } = store.getState().home
    if (hasToken()) {
      await instance.delete(`/user/channels/${id}`)
    } else {
      setChannel(userChannel.filter((item) => item.id !== id))
    }
    dispatch(getUserChannel())
  }
}

export const getArticleList = (
  channel_id: number,
  timestamp: number
): RootThunkAction => {
  return async (dispatch) => {
    const res = await instance.get<
      ApiResponse<{ pre_timestamp: string; results: Article[] }>
    >('/articles', {
      params: {
        channel_id,
        timestamp,
      },
    })
    // console.log(res)
    const { results, pre_timestamp } = res.data.data
    dispatch({
      type: 'home/getArticle',
      payload: {
        channel_id,
        timestamp: pre_timestamp,
        articles: results,
      },
    })
  }
}

export const getNewArticle = (
  channel_id: number,
  timestamp: number
): RootThunkAction => {
  return async (dispatch) => {
    const res = await instance.get<
      ApiResponse<{ pre_timestamp: string; results: Article[] }>
    >('/articles', {
      params: {
        channel_id,
        timestamp,
      },
    })
    // console.log(res)
    const { results, pre_timestamp } = res.data.data
    dispatch({
      type: 'home/getNewArticle',
      payload: {
        channel_id,
        timestamp: pre_timestamp,
        articles: results,
      },
    })
  }
}
